//
//  DriverArrivedTimePickerView.m
//  FMJY
//
//  Created by 艾玩科技 on 17/4/11.
//  Copyright © 2017年 随心. All rights reserved.
//

#import "DriverArrivedTimePickerView.h"

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
#define HEIGHT_OF_POPBOX (([UIScreen mainScreen].bounds.size.width == 414)?290:280)
#define HEIGHT_PICKER HEIGHT_OF_POPBOX - 160 + self.TimeDataSource.count * 20

#define COLOR_BACKGROUD_GRAY    [UIColor colorWithRed:238/255.0f green:238/255.0f blue:238/255.0f alpha:1]
#define bottomView_W 340


@interface DriverArrivedTimePickerView ()<UIPickerViewDataSource, UIPickerViewDelegate>
@property (nonatomic ,strong) UIView * bottomView;       //底层视图
@property (nonatomic ,strong) UIPickerView * statePicker;//运营状态轱辘
//@property (nonatomic ,strong) UIButton * sureBtn;      //确定按钮
@property (nonatomic ,strong) UILabel * titleLabel;      //标题
@property (nonatomic ,strong) UILabel * adressLabel;      //地址
@property (nonatomic ,strong) UILabel * orderTypeLabel;      //救援类型
@property (nonatomic ,strong) NSString * stateStr;       //选中的运营状态
@property (nonatomic, assign) NSInteger pickerHeight_time;    //弹层的高度
@end


@implementation DriverArrivedTimePickerView



- (instancetype)init
{
    if (self = [super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)]) {
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        self.userInteractionEnabled = YES;
        [self addSubview:self.maskView];
        //        self.pickerHeight = HEIGHT_OF_POPBOX - 160;
        //初始化子视图
        [self initSubViews];
    }
    return self;
}

/**初始化子视图*/
- (void)initSubViews
{
    //初始化轱辘的位置
    self.bottomView = [[UIView alloc] init];
    self.bottomView.backgroundColor = [UIColor whiteColor];
    self.bottomView.layer.cornerRadius = 3;
    self.bottomView.layer.masksToBounds =YES;
    [self addSubview:self.bottomView];
    
    
    //标题
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.textAlignment = NSTextAlignmentLeft;
    self.titleLabel.text = self.time_pickerTitle;
    self.titleLabel.font = [UIFont systemFontOfSize:17];
    [self.bottomView addSubview:self.titleLabel];
    
    //救援地址
    self.adressLabel = [[UILabel alloc] init];
    self.adressLabel.textAlignment = NSTextAlignmentLeft;
    self.adressLabel.text =[NSString stringWithFormat:@"救援地址：%@", self.orderAdressStr];
    self.adressLabel.font = [UIFont systemFontOfSize:14];
    self.adressLabel.textColor = [UIColor lightGrayColor];
    [self.bottomView addSubview:self.adressLabel];
    
    //救援类型
    self.orderTypeLabel = [[UILabel alloc] init];
    self.orderTypeLabel.textAlignment = NSTextAlignmentLeft;
    self.orderTypeLabel.text = [NSString stringWithFormat:@"救援类型：%@", self.orderTypeStr];
    self.orderTypeLabel.font = [UIFont systemFontOfSize:14];
    self.orderTypeLabel.textColor = [UIColor lightGrayColor];
    [self.bottomView addSubview:self.orderTypeLabel];
    
    //选择器
    self.statePicker = [[UIPickerView alloc] init];
    self.statePicker.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    self.statePicker.backgroundColor = [UIColor whiteColor];
    self.statePicker.showsSelectionIndicator = YES;
    self.statePicker.dataSource = self;
    self.statePicker.delegate = self;
    [self.bottomView addSubview:self.statePicker];
    
    
    //完成按钮
    self.sureBtn = [TimeButton buttonWithType:UIButtonTypeSystem];
    self.sureBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.sureBtn.titleLabel.font = [UIFont systemFontOfSize:17];
    [self.sureBtn addTarget:self action:@selector(finishBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
//    [self.sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [self.sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.sureBtn.backgroundColor = [UIColor colorWithRed:0.337 green:0.682 blue:0.961 alpha:1.00];
    //加入倒计时
  
    [self.bottomView addSubview:self.sureBtn];
    
    
    
}

- (void)layoutSelfSubviews
{
    //底层视图
    self.bottomView.frame = CGRectMake(20, (KScreenHeight - bottomView_W)/2.0f, SCREEN_WIDTH -40, bottomView_W);
    
    //标题
    self.titleLabel.frame = CGRectMake(30, 10, SCREEN_WIDTH -60, 40);
    
    self.adressLabel.frame = CGRectMake(30, self.titleLabel.bottom , KScreenWidth -60, 20);
    self.orderTypeLabel.frame = CGRectMake(30, self.adressLabel.bottom, KScreenWidth - 60, 20);
    //选择器
    self.statePicker.frame = CGRectMake(0, self.orderTypeLabel.bottom , SCREEN_WIDTH -40, 200);
    
    
    //完成按钮
    
    self.sureBtn.frame = CGRectMake(0, self.statePicker.bottom , KScreenWidth -40, 50);
    
    
    
    
}

#pragma mark - set相关

- (void)setTimeDataSource:(NSArray *)TimeDataSource
{
    _TimeDataSource = TimeDataSource;
    
    //设置弹层高度
    if (HEIGHT_PICKER > HEIGHT_OF_POPBOX - 24) {
        self.pickerHeight_time = HEIGHT_OF_POPBOX - 24;
    } else {
        self.pickerHeight_time = HEIGHT_PICKER;
    }
    
    //设置返回默认值
    self.stateStr = [NSString stringWithFormat:@"%@/%@",_TimeDataSource[0],@(1)];
    
    //刷新布局
    [self layoutSelfSubviews];
    [self.statePicker setNeedsLayout];
    
    //刷新轱辘数据
    [self.statePicker reloadAllComponents];
    [self.statePicker selectRow:0 inComponent:0 animated:NO];
}

- (void)setDefaultStr:(NSString *)defaultStr
{
    _defaultStr = defaultStr;
    
    self.stateStr = defaultStr;
    NSArray * selectArr = [defaultStr componentsSeparatedByString:@"/"];
    [self.statePicker selectRow:[selectArr[1] integerValue] - 1 inComponent:0 animated:NO];
}
//标题
-(void)setTime_pickerTitle:(NSString *)time_pickerTitle
{
    _time_pickerTitle = time_pickerTitle;
    self.titleLabel.text = time_pickerTitle;
}
//救援类型
-(void)setOrderTypeStr:(NSString *)orderTypeStr
{
    _orderTypeStr = orderTypeStr;
    self.orderTypeLabel.text = [NSString stringWithFormat:@"救援类型：%@", orderTypeStr];
}
//救援地址
-(void)setOrderAdressStr:(NSString *)orderAdressStr
{
    _orderAdressStr = orderAdressStr;
    self.adressLabel.text =[NSString stringWithFormat:@"救援地址：%@", orderAdressStr];
    
}
#pragma mark - UIPickerViewDataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.TimeDataSource.count;
}
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 40;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return self.TimeDataSource[row];
}

#pragma mark - UIPickerViewDelegate

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    self.stateStr = [NSString stringWithFormat:@"%@/%@",self.TimeDataSource[row], @(row + 1)];
    
    
}

#pragma mark - 按钮相关

/**点击完成按钮*/
- (void)finishBtnClicked:(UIButton *)button
{
    self.valueDidSelected(self.stateStr);
    [self removeSelfFromSupView_time];
}



/**点击背景释放界面*/
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//    [self removeSelfFromSupView_time];
}

#pragma mark - 显示弹层相关

/**弹出视图*/
- (void)showView
{
    [[[UIApplication sharedApplication].delegate window] addSubview:self];
    
    //动画出现
    CGRect frame = self.bottomView.frame;
    if (frame.origin.y == SCREEN_HEIGHT) {
        //选择器展示的位置
        frame.origin.y = self.pickerHeight_time;
        [UIView animateWithDuration:0.3 animations:^{
            self.bottomView.frame = frame;
        }];
    }
}

/**移除视图*/
- (void)removeSelfFromSupView_time
{
    
    
    [self removeFromSuperview];
}

@end
